package com.moyangfenghun.javabase.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfigure implements WebMvcConfigurer {
    // 注册拦截器
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new HandlerInterceptor() {
//            @Override
//            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//                System.out.println(request.getRequestURL());
//                return HandlerInterceptor.super.preHandle(request, response, handler);
//            }
//        });
//        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
//        registry.addInterceptor(new SaInterceptor(handler->{
//            // 拦截的 path 列表，可以写多个
//            SaRouter.match("/**")
//                    //系统服务全排除
//                    .notMatch("/sys*/*")
//                    // 下边的是knife4j使用的
//                    .notMatch("/**.html")
//                    .notMatch("/v3/api-docs/**")
//                    .notMatch("/webjars/**")
//                    //微信接口
//                    .notMatch("/wx/**")
//                    .notMatch("/ewx/**")
//                    //静态资源
//                    .notMatch("/upload/*")
//                    //测试接口
//                    .notMatch("/test/*")
//                    .check(r -> StpUtil.checkLogin());
//        })).addPathPatterns("/**");
//    }

    @Value("${resourcefile:resourcefile}")
    private String resourcefile="resourcefile";

    @Value("${pathprefix:upload}")
    private String pathprefix;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String file = "";
        if(System.getProperty("user.dir").contains(":")){
            //则时windos系统
            file="file:" + System.getProperty("user.dir") + "/" + resourcefile + "/";
        }else{
            file = "file:" + System.getProperty("user.dir")+ "/" + resourcefile + "/";
        }
        registry.addResourceHandler("/"+pathprefix+"/**")
                .addResourceLocations(file);
        System.out.println("配置的静态资源路径:");
        System.out.println(file);

    }

    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600)
                .exposedHeaders("Authorization");
    }
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setAllowCredentials(true);
        config.setMaxAge(3600L);
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }


}
